/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomPlotChart.java
 *
 * Created on Nov 30, 2010, 7:50:10 PM
 */

package stats;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;

import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.xy.DefaultXYDataset;


import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.*;
import org.jfree.chart.annotations.*;

import org.jfree.chart.title.CompositeTitle;
import org.jfree.chart.title.LegendTitle;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;

import org.jfree.chart.annotations.XYPointerAnnotation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockContainer;
import org.jfree.chart.block.BorderArrangement;
import org.jfree.chart.block.EmptyBlock;


import java.awt.Color;

import java.io.File;
import java.io.IOException;
import org.jfree.chart.ChartUtilities;

/**
 *
 * @author Administrator
 */
public class CustomPlotChart extends javax.swing.JFrame {

    public JFreeChart chart=null;


    /** Creates new form CustomPlotChart */
    public CustomPlotChart(String chartTitle,String XLabel, String YLabel, XYSeriesCollection  _dataset) {
        initComponents();

        //super(applicationTitle);
        this.setTitle(chartTitle);

        // This will create the dataset
        XYSeriesCollection dataset = _dataset;
        // based on the dataset we create the chart
        chart = createChart(dataset,XLabel,YLabel, chartTitle);
        applyChartAttributes(chart);
        // we put the chart into a panel
        ChartPanel chartPanel = new ChartPanel(chart);
        // default size
        chartPanel.setPreferredSize(new java.awt.Dimension(640, 480));
        // chartPanel.addChartMouseListener(this);
        
        // add it to our application
        setContentPane(chartPanel);

    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                        

    /**
    * @param args the command line arguments
    */

    /*
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CustomPlotChart().setVisible(true);
            }
        });
    }
     * 
     */


    /*
    public void chartMouseClicked(ChartMouseEvent e) {
        System.out.println(plot.getRangeCrosshairValue());
        System.out.println(plot.getDomainCrosshairValue());
    }
     * 
     */


     private JFreeChart createChart(XYSeriesCollection dataset, String XLabel,
                                    String YLabel, String title)
     {

        JFreeChart chart = ChartFactory.createScatterPlot(
            title,          // chart title
            XLabel,               // domain axis label
            YLabel,                  // range axis label
            dataset,                  // data
            PlotOrientation.VERTICAL,
            true,                     // include legend
            true,
            true
        );

    
        
        return chart;

    }

    public void saveChartAsPNG(String filename)
    {
        try{
        ChartUtilities.saveChartAsPNG(new File(filename), chart, 640, 480);
        } catch (IOException e)  {  }
    }

    public void appendRenderItemText()
    {
        

    }

    private JFreeChart applyChartAttributes(JFreeChart chart)
    {
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);



        XYItemRenderer r = plot.getRenderer();

        if (r instanceof XYLineAndShapeRenderer) {
            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
            renderer.setBaseShapesVisible(true);
            renderer.setBaseShapesFilled(true);
            renderer.setSeriesPaint(0, Color.BLUE);

            /*
            XYPointerAnnotation annotation = new XYPointerAnnotation(
             ".", 10.0, 10.0, Math.PI / 2.0   );
            //annotation.setTextAnchor(TextAnchor.TOP_RIGHT);
            //annotation.setText("Day la text");
            annotation.setToolTipText("Tooltip");
            renderer.addAnnotation(annotation);
            plot.setRenderer(1, renderer);
             * 
             */

            // create two legends and put them into a composite title
            /*
            LegendTitle legend1 = new LegendTitle(renderer);
            LegendTitle legend2 = new LegendTitle(renderer);
            BlockContainer container = new BlockContainer(new BorderArrangement());
            container.add(legend1, RectangleEdge.LEFT);
            container.add(legend2, RectangleEdge.RIGHT);
            container.add(new EmptyBlock(2000, 0));
            CompositeTitle legends = new CompositeTitle(container);
            legends.setPosition(RectangleEdge.BOTTOM);
            chart.addSubtitle(legends);
             * 
             */

            //renderer.addAnnotation(new XYAnnotation());
        }
        return chart;
    }

    public void setChartText(String msg)
    {
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);



        XYItemRenderer r = plot.getRenderer();

        if (r instanceof XYLineAndShapeRenderer) {
            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
            renderer.setBaseShapesVisible(true);
            renderer.setBaseShapesFilled(true);
            renderer.setSeriesPaint(0, Color.BLUE);

            
            XYPointerAnnotation annotation = new XYPointerAnnotation(
             "A", 1.5, 1.5, Math.PI / 2.0   );
            //annotation.setTextAnchor(TextAnchor.TOP_RIGHT);
            
            annotation.setToolTipText(msg);
            renderer.addAnnotation(annotation);
            plot.setRenderer(1, renderer);
             

            // create two legends and put them into a composite title
            /*
            LegendTitle legend1 = new LegendTitle(renderer);
            LegendTitle legend2 = new LegendTitle(renderer);
            BlockContainer container = new BlockContainer(new BorderArrangement());
            container.add(legend1, RectangleEdge.LEFT);
            container.add(legend2, RectangleEdge.RIGHT);
            container.add(new EmptyBlock(2000, 0));
            CompositeTitle legends = new CompositeTitle(container);
            legends.setPosition(RectangleEdge.BOTTOM);
            chart.addSubtitle(legends);
             *
             */

            //renderer.addAnnotation(new XYAnnotation());
        }
        //return chart;
    }



    // Variables declaration - do not modify                     
    // End of variables declaration                   

}
